package game.Screens;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Vector2f;

import game.Game;
import game.GameHud;
import game.LevelManager;
import game.Actors.Actor;
import game.Actors.Player;
import game.Config.Config;
import game.Config.Config.GameInput;
import game.Config.Storage;
import game.Config.Weaponry;

/**
 * This Screen handles the game when it's running.
 * 
 * @author Sebastian Isheden and Victor Beer
 *
 */
public class GameScreen extends Screen{
	private Player player;
	private Vector2f playerMove = new Vector2f(0,0);
	/**
	 * Is called when the screen is created.
	 * Initiates the player position.
	 */
	@Override
	public void begin() {
		player = new Player(Config.playerImage.copy(), Config.PLAYER_START_POSITION.copy(), Weaponry.weapons.get(0));
		for(int i = 1; i < Weaponry.weapons.size(); i++) {
			player.addWeapon(Weaponry.weapons.get(i).copy());
		}
		Storage.storePlayer(player);
	}

	/**
	 * Is called when the screen is removed.
	 */
	@Override
	public void end() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * Is called when the game updates.
	 * This method handles the input from the keyboard and updates all 
	 * actors on the screen.
	 * @param gc The GameContainer
	 * @param delta The time in milliseconds since the last update.
	 */
	@Override
	public void update(GameContainer gc, int delta) {
		LevelManager.updateCurrentWave(delta);
		Input input = gc.getInput();
		player.move(playerMove);
		playerMove = new Vector2f(0,0);
		player.setShootDirection(input.getMouseX(),input.getMouseY());
		Actor.updateAllActors(delta);
		GameHud.updateHud(gc, delta);
		
		checkRemainingEggs();
	}
	private void checkRemainingEggs() {
		if(LevelManager.checkLoss()){
			Game.screenStack.pop();
			Game.pushNewScreen(new DefeatScreen());
		}
		
	}

	/**
	 * Draws all the actors to the screen.
	 */
	@Override
	public void draw(Graphics graphic) {
		LevelManager.drawCurrentLevel(graphic);
		Actor.drawAllActors();	
		GameHud.drawHud(graphic);
		LevelManager.drawWaveStatus(graphic);
	}

	@Override
	public void inputPressed(GameInput input) {
		if(input == GameInput.ACTION3){
			player.swapWeapon();
		}
	}

	@Override
	public void inputDown(GameInput input) {
		if(input == GameInput.LEFT){
			playerMove.x += -Config.PLAYER_SPEED;
		} else if(input == GameInput.UP){
			playerMove.y += -Config.PLAYER_SPEED;
		} else if(input == GameInput.DOWN){
			playerMove.y += Config.PLAYER_SPEED;
		} else if(input == GameInput.RIGHT){
			playerMove.x += Config.PLAYER_SPEED;
		} else if(input == GameInput.ACTION){
			player.shoot();
		} else if(input == GameInput.BUILD){
			if(player.canBuild()){
				Game.pushNewScreen(new TowerScreen());
			}else if(player.canUpgrade()){
				Game.pushNewScreen(new TowerUpgradeScreen());
			}
		}
		
	}

}
